<html>
<meta charset="UTF-8"> <!-- for HTML5 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
    <title>test</title>
</head>
<body>
<form onsubmit="return false;">
    <input type="text" name="message" style="width: 600px;">
    <button onclick="send(this.form.message.value)">send</button>
    <br>
    <h3>return messages</h3>
    <textarea id="returns" rows="10" cols="100"></textarea>
</form>
</body>
<script type="text/javascript">
    var socket
    if(!window.WebSocket){
        window.WebSocket = window.MozWebSocket
    }
    //"62.234.180.200";//
    var server = (window.location.host).split(":")[0];
    if(window.WebSocket){
        socket = new WebSocket("ws://"+server+":9961/websocket?token=token1&type=2&userId=123456&toId=9999")
        socket.onmessage = function(event){
            var returns = document.getElementById("returns")
            returns.value = returns.value + "\n"
            returns.value = returns.value + event.data
        }
        socket.onopen = function(event){
            var returns = document.getElementById("returns")
            returns.value = returns.value + "\n"
            returns.value = returns.value + "已经连接上WebSocket服务器"
        }
        socket.onclose = function(event){
            var returns = document.getElementById("returns")
            returns.value = returns.value + "\n"
            returns.value = returns.value + "断开与Socket服务器的连接"
        }
    }
    function send(msg){
        if(!socket){
            alert()
            return
        }
        if(socket.readyState == WebSocket.OPEN){
            socket.send(msg)
        }else{
            alert("WebSocket还未连接成功")
        }
    }
</script>
</html>
